package main

import (
	"fmt"
	"os"
	_ "xiegao/caches"
	_ "xiegao/common"
	_ "xiegao/filters"
	"xiegao/models"
	_ "xiegao/routers"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	//beego.Run()
	beego.SetStaticPath("/upload", "upload")
	beego.BConfig.WebConfig.DirectoryIndex = true
	beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
	args := os.Args[1:]
	if len(args) == 0 {
		beego.Run()
	}
	switch args[0] {
	case "runserver":
		beego.Run()
		break
	case "createsuperuser":
		user := &models.User{Username: args[1]}
		user.SetPassword(args[2])
		o := orm.NewOrm()
		_, error := o.Insert(user)
		if error == nil {
			fmt.Printf("用户  %s 新建成功", user.Username)
		} else {
			fmt.Println(error)
			fmt.Printf("用户  %s 新建失败", user.Username)
		}
		break
	default:
		beego.Run()
		break
	}
}

func init() {
	mysql_addr := beego.AppConfig.String("database_uri")
	fmt.Println(mysql_addr)
	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("default", "mysql", mysql_addr,)
	orm.RunSyncdb("default", false, false)
}
